package org.csu.jpetstore_users_backend_pj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.csu.jpetstore_users_backend_pj.entity.Category;
import org.csu.jpetstore_users_backend_pj.entity.Item;
import org.csu.jpetstore_users_backend_pj.entity.ItemQuantity;
import org.csu.jpetstore_users_backend_pj.entity.Product;
import org.csu.jpetstore_users_backend_pj.mapper.CategoryMapper;
import org.csu.jpetstore_users_backend_pj.mapper.ItemMapper;
import org.csu.jpetstore_users_backend_pj.mapper.ItemQuantityMapper;
import org.csu.jpetstore_users_backend_pj.mapper.ProductMapper;
import org.csu.jpetstore_users_backend_pj.service.CatalogService;
import org.csu.jpetstore_users_backend_pj.vo.*;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service("catalogService")
@MapperScan("org.csu.petstore.mapper")
public class CatalogServiceImpl implements CatalogService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ItemMapper itemMapper;
    @Autowired
    private ItemQuantityMapper itemQuantityMapper;

//    @Override
//    public List<Category> getCategories() {
//        return categoryMapper.selectList(null);
//    }

    @Override
    public CategoryVO getCategory(String categoryId) {
        CategoryVO categoryVO = new CategoryVO();
        Category category = categoryMapper.selectById(categoryId);
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("categoryid", categoryId);
        List<Product> productList = productMapper.selectList(queryWrapper);

        categoryVO.setCategoryId(categoryId);
        categoryVO.setCategoryName(category.getName());
        categoryVO.setProductList(productList);

        return categoryVO;
    }

    @Override
    public List<CategoriesVO> getCategories() {
        // 获取所有分类
        List<Category> categories = categoryMapper.selectList(null);
        // 转换为VO对象
        return categories.stream().map(category -> {
            CategoriesVO vo = new CategoriesVO();
            vo.setCategoryId(category.getCategoryId());
            vo.setCategoryName(category.getName());
            vo.setImgUrl(category.getImg());
            return vo;
        }).collect(Collectors.toList());
    }

    @Override
    public ProductVO getProduct(String productId) {
        ProductVO productVO = new ProductVO();
        Product product = productMapper.selectById(productId);
        QueryWrapper<Item> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("productid", productId);
        List<Item> itemList = itemMapper.selectList(queryWrapper);

        productVO.setProductId(productId);
        productVO.setCategoryId(product.getCategoryId());
        productVO.setProductName(product.getName());
        productVO.setItemList(itemList);
        return productVO;
    }

    @Override
    public ItemVO getItem(String itemId) {
        ItemVO itemVO = new ItemVO();
        Item item = itemMapper.selectById(itemId);
        Product product = productMapper.selectById(item.getProductId());
        ItemQuantity itemQuantity = itemQuantityMapper.selectById(itemId);
        itemVO.setItemId(itemId);
        itemVO.setListPrice(item.getListPrice());
        itemVO.setAttribute1(item.getAttribute1());
        itemVO.setProductId(product.getProductId());
        itemVO.setProductName(product.getName());
        itemVO.setQuantity(itemQuantity.getQuantity());

//        String[] temp = product.getDescription().split("\"");
        itemVO.setDescriptionImage(item.getImg());
        //itemVO.setDescriptionImage(item.getImg());
        itemVO.setDescriptionText(product.getDescription());
        itemVO.setProduct(getProduct(product.getProductId()));
        return itemVO;
    }

    @Override
    public boolean isItemInStock(String itemId) {
        ItemQuantity itemQuantity = itemQuantityMapper.selectById(itemId);
        if (itemQuantity != null) {
            return itemQuantity.getQuantity() > 0;
        }
        else return false;
    }

    @Override
    public List<Product> searchProductList(String keyword) {
        return productMapper.searchProductList(keyword);
    }

    @Override
    public InfoWindowCategoryVO getInfoWindowCategoryVO(String categoryId){
        InfoWindowCategoryVO infoWindowCategoryVO = new InfoWindowCategoryVO();
        Category category = categoryMapper.selectById(categoryId);
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("categoryid", categoryId);
        List<Product> productList = productMapper.selectList(queryWrapper);
        infoWindowCategoryVO.setCategoryName(category.getName());
        infoWindowCategoryVO.setProductList(productList);
        return infoWindowCategoryVO;
    }

    @Override
    public String updateImgSrc(String productId) {
        Product product=productMapper.selectById(productId);
        String img=product.getImg();
        String description=product.getDescription();
        String []temp=description.split("\"");
        System.out.println("split result:");
        for (int i = 0; i < temp.length; i++) {
            System.out.println(temp[i]);
        }
        String newdesc=temp[0]+"\""+img+"\""+temp[2];
        product.setDescription(newdesc);
        productMapper.updateById(product);
        return null;
    }
}
